package com.wm.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.reggie.common.CustomException;
import com.wm.reggie.entity.Category;
import com.wm.reggie.entity.Dish;
import com.wm.reggie.entity.Setmeal;
import com.wm.reggie.mapper.CategoryMapper;
import com.wm.reggie.service.CategoryService;
import com.wm.reggie.service.DishService;
import com.wm.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 删除条件
     * @param id
     */
    @Override
    public void remove(Long id) {
        //查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //查询菜品关联的，如果已经关联，抛出 一个异常
        if(count1 > 0){
            //抛出一个异常
            throw new CustomException("已经关联菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        //查询套餐关联的，如果已经关联，抛出一个异常
        if(count2 > 0){
            //抛出一个异常
            throw new CustomException("已经关联套餐，不能删除");
        }

        //如果都没有问题
        //正常删除
        super.removeById(id);
    }
}
